#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
struct node{
ll x,y,pn;
}num[64];
int n,m;
node dfs(ll x){
int flag=0;
if(x==0)return {0,0,1};
if(x==1)return {m,m,n};x++;
if(1ll<<__builtin_ctzll(x)==x)flag=1;
if(flag&&num[__builtin_ctzll(x)].pn)
return num[__builtin_ctzll(x)];
x--;
ll mid=x-((1ll<<63-__builtin_clzll(x))-1),s=63-__builtin_clzll(x);
ll l=min(mid,1ll<<s-1),r=mid-l;
l+=(x-mid)/2;r+=(x-mid)/2;
auto ls=dfs(l),rs=dfs(r);
node now;
now.pn=ls.pn*rs.pn%mod*n%mod;
now.y=(ls.y*rs.pn+rs.y*ls.pn+ls.pn*rs.pn)%mod*m%mod;
ls.pn=ls.pn*n%mod;rs.pn=rs.pn*n%mod;
now.x=(ls.x*rs.pn+rs.x*ls.pn+ls.y*rs.y%mod*m+now.y)%mod;
x++;
if(flag)num[__builtin_ctzll(x)]=now;
return now;
}
int main(){
int t;scanf("%d",&t);
while(t--){
ll k,ans=0,mid=0;
scanf("%lld%d",&k,&n);
for(m=1;m<=n;m++){
memset(num,0,sizeof(num));
ll op=dfs(k).x;
//printf("%lld\n",op);
ans+=m*(op-mid+mod);
mid=op;
}
printf("%lld\n",ans%mod);
}
}
1588. Sum of All Odd Length Subarrays | 1662. Check If Two String Arrays are Equivalent |
1832. Check if the Sentence Is Pangram | 1678. Goal Parser Interpretation |
1389. Create Target Array in the Given Order | 1313. Decompress Run-Length Encoded List |
1281. Subtract the Product and Sum of Digits of an Integer | 1342. Number of Steps to Reduce a Number to Zero |
1528. Shuffle String | 1365. How Many Numbers Are Smaller Than the Current Number |
771. Jewels and Stones | 1512. Number of Good Pairs |
672. Richest Customer Wealth | 1470. Shuffle the Array |
1431. Kids With the Greatest Number of Candies | 1480. Running Sum of 1d Array |
682. Baseball Game | 496. Next Greater Element I |
232. Implement Queue using Stacks | 844. Backspace String Compare |
20. Valid Parentheses | 746. Min Cost Climbing Stairs |
392. Is Subsequence | 70. Climbing Stairs |
53. Maximum Subarray | 1527A. And Then There Were K |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers | 318. Maximum Product of Word Lengths |
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |